''''
    4月新增：关键字：逻辑代码二次封装
'''

import requests
from conf.read_conf import get_headers, get_url
import logging


# 定义接口关键字驱动类
class ApiKeys:
    # get请求
    def do_get(self,envs, path, headers=None, params=None, **kwargs):
        # 调用拼接url方法
        url = self.set_url(envs,path)
        # 调用请求头的处理方法
        headers = get_headers(envs,headers)
        # 发送请求
        response = requests.get(url=url, headers=headers, params=params, **kwargs,verify=False)
        # 返回响应结果
        logging.info("请求地址：%s 请求数据data：%s 响应状态码：%s 请求头：%s",
                     url, params, response.status_code, response.request.headers)
        return response

    # post请求
    def do_post(self, envs,path, headers=None, data=None, json=None, **kwargs):
        url = self.set_url(envs,path)
        headers = get_headers(envs,headers)
        if data is not None:
            response = requests.post(url=url, headers=headers, data=data, **kwargs,verify=False)
        else:
            response = requests.post(url=url, headers=headers, json=json, **kwargs,verify=False)
        logging.info("请求地址：%s | 数据(data)：%s | 数据(json)：%s | 状态码：%s | 请求头：%s",
                     url, data, json, response.status_code, response.request.headers)
        return response

    # url的拼接处理
    def set_url(self,envs, path=None):
        url = get_url(envs)
        # 存在path就添加
        if path:
            # http://ip:port/path
            url = url + path
        return url

